////
//
/// \file ./test/block_entry_node_test.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/block_entry_node.hpp>

#include <inttypes.h>
#include <string>

#include <gtest/gtest.h>

#include <inc/parser/utilities.hpp>

namespace inc {
namespace parser {

using std::string;

TEST(BlockEntryNodeTest, parse_statement)
{
  const string data = "a := b\n";
  BlockEntryNode node;
  string::const_iterator last_parsed = data.begin();
  EXPECT_TRUE( node.parse( data.begin(), data.end(), last_parsed ) );
  EXPECT_EQ( data, string( data.begin(), last_parsed ) );
  EXPECT_EQ( data, node.get_text() );
  EXPECT_TRUE( node.is_statement() );
  EXPECT_FALSE( node.is_if_statement() );
  EXPECT_FALSE( node.is_local_decl() );
}

TEST(BlockEntryNodeTest, parse_bad_statement)
{
  const string data = "(a + b)\n";
  BlockEntryNode node;
  string::const_iterator last_parsed = data.end();
  EXPECT_FALSE( node.parse( data.begin(), data.end(), last_parsed ) );
  EXPECT_EQ( string(), string( data.begin(), last_parsed ) );
  EXPECT_FALSE( node.is_statement() );
  EXPECT_FALSE( node.is_if_statement() );
  EXPECT_FALSE( node.is_local_decl() );
}

TEST(BlockEntryNodeTest, parse_if_statement)
{
  const string data = "if x then a := b\n";
  BlockEntryNode node;
  string::const_iterator last_parsed;
  EXPECT_TRUE( node.parse( data.begin(), data.end(), last_parsed ) );
  EXPECT_EQ( data, string( data.begin(), last_parsed ) );
  EXPECT_EQ( data, node.get_text() );
  EXPECT_TRUE( node.is_if_statement() );
  EXPECT_FALSE( node.is_statement() );
  EXPECT_FALSE( node.is_local_decl() );
}

TEST(BlockEntryNodeTest, parse_bad_if_statement)
{
  const string data = "iff x then a := b\n";
  BlockEntryNode node;
  string::const_iterator last_parsed = data.end();
  EXPECT_FALSE( node.parse( data.begin(), data.end(), last_parsed ) );
  EXPECT_EQ( string(), string( data.begin(), last_parsed ) );
  EXPECT_FALSE( node.is_statement() );
  EXPECT_FALSE( node.is_if_statement() );
  EXPECT_FALSE( node.is_local_decl() );
}

TEST(BlockEntryNodeTest, parse_local_decl)
{
  const string data = "local x is float\n";
  BlockEntryNode node;
  string::const_iterator last_parsed;
  EXPECT_TRUE( node.parse( data.begin(), data.end(), last_parsed ) );
  EXPECT_EQ( data, string( data.begin(), last_parsed ) );
  EXPECT_EQ( data, node.get_text() );
  EXPECT_TRUE( node.is_local_decl() );
  EXPECT_FALSE( node.is_statement() );
  EXPECT_FALSE( node.is_if_statement() );
}

TEST(BlockEntryNodeTest, parse_bad_local_decl)
{
  const string data = "local x isn't a float\n";
  BlockEntryNode node;
  string::const_iterator last_parsed = data.end();
  EXPECT_FALSE( node.parse( data.begin(), data.end(), last_parsed ) );
  EXPECT_EQ( string(), string( data.begin(), last_parsed ) );
  EXPECT_FALSE( node.is_statement() );
  EXPECT_FALSE( node.is_if_statement() );
  EXPECT_FALSE( node.is_local_decl() );
}

} //  end parser namespace
} //  end inc namespace
